import pandas as pd
import numpy as np
import pyecharts.options as opts
from pyecharts.charts import Line, Timeline
# from pyecharts.commons.utils import JsCode

class LineChart_flow_timeline():
    
    def __init__(self, dataset, place=0):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1
        # self.year=year

        #主程序入口
        self.main()
    
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘图函数
        self.paint()
        
        
    def getData(self):
            
        self.paint_data=self.paint_data.loc[:,['date','flow']] #提取日期和客流数据
        # self.paint_data=df.loc[df['date'].str.contains(self.year)] #筛选指定年份的数据


    def paint(self):
        
        # background_color_js = (
        #     "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
        #     "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
        # )
        # area_color_js = (
        #     "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
        #     "[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
        # )
        
        if self.place==0:
            place='九寨沟'
            years=range(2013, 2017) #设置年份范围
            fileName="1.2 九寨沟2013-2016年客流折线图（含时间轴）.html"
        else:
            place='四姑娘山'
            years=range(2016, 2020) #设置年份范围
            fileName="1.2 四姑娘山2016-2019年客流折线图（含时间轴）.html"
        
        tl = Timeline()
        for i in years:
            data=self.paint_data.loc[self.paint_data['date'].str.contains(str(i))] #筛选指定年份的数据
            # print(data)
            # print(data['date'])
            x_data = np.array(data['date']).tolist()
            # print(x_data)
            y_data = np.array(data['flow']).tolist()    
            
            c = (
                Line(init_opts=opts.InitOpts(width="1400px", height="600px"))
                .add_xaxis(xaxis_data=x_data)
                .add_yaxis(
                    "客流",
                    y_data,
                    symbol="triangle",
                    symbol_size=8,
                    linestyle_opts=opts.LineStyleOpts(color="#00CED1", width=1.5),
                    itemstyle_opts=opts.ItemStyleOpts(
                        border_width=1, border_color="#000000", color="#000000"
                    ),
                )
                .set_global_opts(title_opts=opts.TitleOpts(title=place+" {} 年客流量".format(i)))
            )
            
            # c = (
            #     Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js),width="900px", height="600px"))
            #     .add_xaxis(xaxis_data=x_data)
            #     .add_yaxis(
            #         series_name="单日客流量",
            #         y_axis=y_data,
            #         is_smooth=True,
            #         is_symbol_show=True,
            #         symbol="circle",
            #         symbol_size=6,
            #         linestyle_opts=opts.LineStyleOpts(color="#fff"),
            #         label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
            #         itemstyle_opts=opts.ItemStyleOpts(
            #             color="red", border_color="#fff", border_width=3
            #         ),
            #         tooltip_opts=opts.TooltipOpts(is_show=False),
            #         areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
            #     )
            #     .set_global_opts(
            #         title_opts=opts.TitleOpts(
            #             title=place+" {} 年客流量".format(i),
            #             pos_bottom="5%",
            #             pos_left="center",
            #             title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
            #         ),
            #         xaxis_opts=opts.AxisOpts(
            #             type_="category",
            #             boundary_gap=False,
            #             axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
            #             axisline_opts=opts.AxisLineOpts(is_show=False),
            #             axistick_opts=opts.AxisTickOpts(
            #                 is_show=True,
            #                 length=25,
            #                 linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
            #             ),
            #             splitline_opts=opts.SplitLineOpts(
            #                 is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
            #             ),
            #         ),
            #         yaxis_opts=opts.AxisOpts(
            #             type_="value",
            #             position="right",
            #             axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
            #             axisline_opts=opts.AxisLineOpts(
            #                 linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
            #             ),
            #             axistick_opts=opts.AxisTickOpts(
            #                 is_show=True,
            #                 length=15,
            #                 linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
            #             ),
            #             splitline_opts=opts.SplitLineOpts(
            #                 is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
            #             ),
            #         ),
            #         legend_opts=opts.LegendOpts(is_show=False),
            #     )
            # )
            tl.add(c, "{}年".format(i))
        tl.render('resultFile/'+fileName)
        
        print(fileName+'已生成！')

